package com.zzn.bookManage.dao.impl;

import com.zzn.bookManage.dao.UserDao;
import com.zzn.bookManage.exceptions.MyException;
import com.zzn.bookManage.pojo.User;
import com.zzn.bookManage.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    private final Connection conn = DBUtil.getConnection();

    @Override
    public List<User> listUsers() {
        try {
            PreparedStatement psmt = conn.prepareStatement("select * from `users`");
            return warpUsers(psmt.executeQuery());
        } catch (SQLException throwables) {
            throw new MyException(throwables.getMessage());
        }
    }

    @Override
    public List<User> listUsersByUsername(User in) {
        try {
            PreparedStatement psmt = conn.prepareStatement("select * from `users` where `username` = ?");
            psmt.setString(1,in.getUsername());
            return warpUsers(psmt.executeQuery());
        } catch (SQLException throwables) {
            throw new MyException(throwables.getMessage());
        }
    }

    @Override
    public boolean updateUserPassword(User in) {
        try {
            PreparedStatement psmt = conn.prepareStatement("update `users` set `password` = ? where `uid` = ?");
            psmt.setString(1,in.getPassword());
            psmt.setInt(2,in.getUid());
            int i = psmt.executeUpdate();
            if (i==0){
                throw new SQLException("==== updateUser 失败");
            }else{
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateUserInfo(User in) {
        try {
            PreparedStatement psmt = conn.prepareStatement("update `users` set `nickname` = ? where `uid` = ?");
            psmt.setString(1,in.getNickname());
            psmt.setInt(2,in.getUid());
            int i = psmt.executeUpdate();
            if (i==0){
                throw new SQLException("==== updateUser 失败");
            }else{
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteUser(User in) {
        try {
            PreparedStatement psmt = conn.prepareStatement("delete from `users` where `uid` = ?");
            psmt.setInt(1,in.getUid());
            int i = psmt.executeUpdate();
            if (i==0){
                throw new SQLException("==== deleteUser 失败");
            }else{
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean addUser(User in) {
        try {
            PreparedStatement psmt = conn.prepareStatement("insert into `users` (`username`,`nickname`,`password`) values (?,?,?)");
            psmt.setString(1,in.getUsername());
            psmt.setString(2,in.getNickname());
            psmt.setString(3,in.getPassword());
            int i = psmt.executeUpdate();
            if (i==0){
                throw new SQLException("==== addUser 失败");
            }else{
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    /**
     * 从结果集中组装 User 列表
     * @param res
     * @return List<User>
     * @throws SQLException
     */
    public static List<User> warpUsers(ResultSet res) throws SQLException {
        List<User> users = new ArrayList<>();
        while (res.next()) {
           users.add(wrapUser(res));
        }
        return users;
    }

    /**
     * 从结果集中组装 User
     * @param res
     * @return User
     * @throws SQLException
     */
    public static User wrapUser(ResultSet res) throws SQLException {
        User user = new User();
        user.setUid(res.getInt("uid"));
        user.setUsername(res.getString("username"));
        user.setPassword(res.getString("password"));
        user.setNickname(res.getString("nickname"));
        return user;
    }
}
